//
//  BanBu_SendMessageViewController.m
// Halfarea
//
//  Created by banbu3 on 14-6-16.
//
//

#import "BanBu_SendMessageViewController.h"

@interface BanBu_SendMessageViewController ()
{
    UIWebView *phoneCallWebView;
}
@end

@implementation BanBu_SendMessageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithPersonData:(NSDictionary *)_dic
{
    self = [super init];
    if (self) {
        _personDictionary = [[NSMutableDictionary alloc] initWithDictionary:_dic];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [sendInvite setTitle:NSLocalizedString(@"sendinvite", 发送邀请) forState:UIControlStateNormal];
    
    phone.text = NSLocalizedString(@"phonenum", 手机号);

    bgButton.layer.cornerRadius = 5;
    
    name.text = _personDictionary[@"personName"];
    
    phoneNumber.text = [_personDictionary[@"phoneNumberArr"] componentsJoinedByString:@"\n"];
    
    NSInteger count = [_personDictionary[@"phoneNumberArr"] count];
    
    self.title = NSLocalizedString(@"yaoqingFriend", nil);
    
    while(count > 1)
    {
        CGRect frame = phoneNumber.frame;
        frame.size.height += 30;
        phoneNumber.frame = frame;
        phoneNumber.numberOfLines = 0;
        
        CGRect bgFrame = bgButton.frame;
        bgFrame.size.height += 30;
        bgButton.frame = bgFrame;
        count -- ;
    }
    //6.16到此为止
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)call:(id)sender
{
    if([_personDictionary[@"phoneNumberArr"] count]>1)
    {
        
        NSArray *list = _personDictionary[@"phoneNumberArr"];
        
        UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"", 请选择要拨打的电话号码) delegate:self cancelButtonTitle:nil
         destructiveButtonTitle:nil  otherButtonTitles:nil];
        for(NSString *title in list)
        {
            [actionsheet addButtonWithTitle:title];
        }
        [actionsheet addButtonWithTitle:NSLocalizedString(@"取消", nil) ];
        [actionsheet showInView:self.view];
        [actionsheet release];
    }
    else
    {
        [self startCall:[_personDictionary[@"phoneNumberArr"] objectAtIndex:0]];
    }
}

- (void)startCall:(NSString *)number
{
    NSString *phoneNum = number;// 电话号码
    
    NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNum]];
    
    phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
    
    [phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];
    
}
- (IBAction)sendInvite:(id)sender
{
    
    NSString* message = [NSString stringWithFormat:@"%@http://www.halfarea.com/beta/",NSLocalizedString(@"inviteFriend", nil)];
    NSString* number = @"";
    MFMessageComposeViewController* controller = [[MFMessageComposeViewController alloc] init];
    if ([MFMessageComposeViewController canSendText]) {
        controller.body =  message;
        //parse through receipients
        NSScanner* numberScanner = [NSScanner scannerWithString:number];
        [numberScanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@","]];
        NSCharacterSet* charactersToSkip = [NSCharacterSet characterSetWithCharactersInString:@","];
        NSString* substring = @"";
        NSMutableArray *substrings = [NSMutableArray array];
        while (![numberScanner isAtEnd]) {
            [numberScanner scanUpToCharactersFromSet:charactersToSkip intoString:&substring];
            [numberScanner scanCharactersFromSet:charactersToSkip intoString:NULL];
            NSLog(@"%@", substring);
            [substrings addObject:substring];
        }
        
        controller.recipients = [NSArray arrayWithObject:[_personDictionary[@"phoneNumberArr"] objectAtIndex:0]];
        controller.messageComposeDelegate = self;
        [self presentViewController:controller animated:YES completion:nil];
    }

}
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissViewControllerAnimated:YES completion:nil];
    if(result == MessageComposeResultCancelled)
        
        
        if (result == MessageComposeResultCancelled)
        {
            NSLog(@"Message cancelled");
        }
        else if (result == MessageComposeResultSent)
        {
            NSLog(@"Message sent");
        }
        else
        {
            NSLog(@"Message failed");
        }
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [name release];
    [phoneNumber release];
    [sendInvite release];
    [phone release];
    [_personDictionary release],_personDictionary = nil;
    if(phoneCallWebView){ [phoneCallWebView release],phoneCallWebView = nil; }
    [bgButton release];
    [super dealloc];
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if([_personDictionary[@"phoneNumberArr"] count] - 1 >buttonIndex)
    {
        [self startCall:[_personDictionary[@"phoneNumberArr"] objectAtIndex:buttonIndex]];

    }
}
@end
